package com.superMarket.project.business;

import com.superMarket.project.domain.ConsumptionReduction;
import com.superMarket.project.domain.Customer;
import com.superMarket.project.domain.Fruit;
import com.superMarket.project.domain.ShoppingRecord;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 购买商品业务类
 * @author mrguo
 * @date 2024/11/04
 */
public class BuyGoodsService {
    /**
     * 获取水果集合数据
     * @param salesScenario 销售场景 1.顾客A买商品场景 2.顾客B买商品场景 3.顾客C买商品场景 4.顾客4买商品场景
     * @return
     */
    public static List<Fruit> getFruitList(String salesScenario){
        List<Fruit> fruitList = new ArrayList<>();
        if("1".equals(salesScenario)){
            fruitList.add(new Fruit("苹果", 8d, 1d));
            fruitList.add(new Fruit("草莓", 13d, 1d));
        }else if("2".equals(salesScenario)){
            fruitList.add(new Fruit("苹果", 8d, 1d));
            fruitList.add(new Fruit("草莓", 13d, 1d));
            fruitList.add(new Fruit("芒果", 20d, 1d));
        }else if("3".equals(salesScenario)){
            fruitList.add(new Fruit("苹果", 8d, 1d));
            fruitList.add(new Fruit("草莓", 13d, 0.8d));
            fruitList.add(new Fruit("芒果", 20d, 1d));
        }else if("4".equals(salesScenario)){
            fruitList.add(new Fruit("苹果", 8d, 1d));
            fruitList.add(new Fruit("草莓", 13d, 0.8d));
            fruitList.add(new Fruit("芒果", 20d, 1d));
        }

        return fruitList;
    }

    /**
     * 计算商品总价
     * @param customer
     * @param fruitList
     * @param recordList 购买记录集合
     * @param reduction 购物满减实体类
     * @return
     */
    public static Map<String, Double> calculateTotalPriceOfGoods(Customer customer, List<Fruit> fruitList, List<ShoppingRecord> recordList, ConsumptionReduction reduction){
        // 原总价
        double originalTotalPrice = 0d;
        // 减免后的总价
        double totalPrice = 0d;

        //定义购物总价
        Map<String, Double> priceMap = new HashMap<>();
        priceMap.put("originalTotalPrice", originalTotalPrice);
        priceMap.put("totalPrice", totalPrice);

        //集合数据为空，直接返回
        if (customer == null
                || (fruitList == null || fruitList.size() == 0)
                || (recordList == null || recordList.size() == 0)){
            return priceMap;
        }

        Map<String, Fruit> fruitMap = fruitList.stream().collect(Collectors.toMap(Fruit::getName, Function.identity()));

        //购物总价达到多少减免
        double reduceStandard = 0d;
        //减免金额
        double reduceAmount = 0d;
        if(reduction != null) {
            //购物总价达到多少减免
            reduceStandard = reduction.getReduceStandard();
            //减免金额
            reduceAmount = reduction.getReduceAmount();
        }

        //遍历集合计算商品总价
        for(ShoppingRecord record : recordList){
            if (record == null || fruitMap.get(record.getFruitName()) == null){
                continue;
            }
            //商品对象
            Fruit fruit = fruitMap.get(record.getFruitName());
            //商品折扣
            double discount = fruit.getDiscount();

            //累加到商品总价
            originalTotalPrice += fruit.getPrice() * record.getBuyQuantity() * discount;
        }

        priceMap.put("originalTotalPrice", originalTotalPrice);

        //处理购物满总价达到多少减免金额的业务
        if (originalTotalPrice > 0d && reduceStandard > 0d && reduceAmount > 0d){
            if (originalTotalPrice >= reduceStandard){
                totalPrice = originalTotalPrice - reduceAmount;
            }else {
                totalPrice = originalTotalPrice;
            }
        }else {
            totalPrice = originalTotalPrice;
        }

        priceMap.put("totalPrice", totalPrice);

        return priceMap;
    }
}
